package com.tianyu.inspection.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.common.component.Validate;
import com.tianyu.inspection.entity.ClientSoftware;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.Ro;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.service.api.IClientSoftwareService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2021/2/23 - 15:56
 **********************************/

@RestController
@RequestMapping("/api/2.0/clientSoftware")
public class ClientSoftwareController {

    @Resource
    private IClientSoftwareService clientSoftwareService;

    @Resource
    private Validate validateModel;

    @GetMapping(value = "/admin/list")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<IPage<ClientSoftware>> getPageList(HttpServletRequest request) {
        QueryMo queryMo = QueryMo.getQueryMo(request);
        Ro<IPage<ClientSoftware>> ro = new Ro<>();
        queryMo.build(ClientSoftware.class, null);
        return ro.setContent(clientSoftwareService.searchListPage(queryMo));
    }

    @GetMapping(value = "/version/{versionNumber}")
    @Permission(value = 0, tags = Constants.API_COMMON)
    public Ro<ClientSoftware> getOne(@PathVariable String versionNumber, HttpServletRequest request) {
        Ro<ClientSoftware> ro = new Ro<>();
        QueryMo queryMo = QueryMo.getQueryMo(request);
        if (Objects.isNull(queryMo.get("type"))) {
            queryMo.put("type", 1);
        }
        if (Objects.isNull(versionNumber)) {
            return ro.setResultEnum(RoState.PARAMS_MISS_ERROR);
        }
        queryMo.put("versionNumber", versionNumber);
        queryMo.build(ClientSoftware.class, null);
        ClientSoftware clientSoftware = clientSoftwareService.selectOne(queryMo);
        if (Objects.isNull(clientSoftware)) {
            ro.setMessage("未查询到数据");
        } else {
            ro.setContent(clientSoftware);
        }
        return ro;
    }

    @GetMapping(value = "/latestVersion")
    @Permission(value = 0, tags = Constants.API_COMMON)
    public Ro<ClientSoftware> getLatestVersion(HttpServletRequest request) {
        Ro<ClientSoftware> ro = new Ro<>();
        QueryMo queryMo = QueryMo.getQueryMo(request);
        if (Objects.isNull(queryMo.get("type"))) {
            queryMo.put("type", 1);
        }
        ClientSoftware clientSoftware = clientSoftwareService.getLatestVersion(queryMo);
        if (Objects.isNull(clientSoftware)) {
            ro.setMessage("未查询到数据");
        } else {
            ro.setContent(clientSoftware);
        }
        return ro;
    }


    @GetMapping(value = "/test/latestVersion")
    @Permission(value = 0, tags = Constants.API_COMMON)
    public Ro<ClientSoftware> getTestLatestVersion(HttpServletRequest request) {
        Ro<ClientSoftware> ro = new Ro<>();
        QueryMo queryMo = QueryMo.getQueryMo(request);
        if (Objects.isNull(queryMo.get("type"))) {
            queryMo.put("type", 2);
        }
        ClientSoftware clientSoftware = clientSoftwareService.getLatestVersion(queryMo);
        if (Objects.isNull(clientSoftware)) {
            ro.setMessage("未查询到数据");
        } else {
            ro.setContent(clientSoftware);
        }
        return ro;
    }

    @GetMapping(value = "/android/latestVersion")
    @Permission(value = 0, tags = Constants.API_PATROL)
    public Ro<ClientSoftware> getAndroidLatestVersion(HttpServletRequest request) {
        Ro<ClientSoftware> ro = new Ro<>();
        QueryMo queryMo = QueryMo.getQueryMo(request);
        if (Objects.isNull(queryMo.get("type"))) {
            queryMo.put("type", 3);
        }
        ClientSoftware clientSoftware = clientSoftwareService.getLatestVersion(queryMo);
        if (Objects.isNull(clientSoftware)) {
            ro.setMessage("未查询到数据");
        } else {
            ro.setContent(clientSoftware);
        }
        return ro;
    }

    @GetMapping(value = "/android/test/latestVersion")
    @Permission(value = 0, tags = Constants.API_PATROL)
    public Ro<ClientSoftware> getAndroidTestLatestVersion(HttpServletRequest request) {
        Ro<ClientSoftware> ro = new Ro<>();
        QueryMo queryMo = QueryMo.getQueryMo(request);
        if (Objects.isNull(queryMo.get("type"))) {
            queryMo.put("type", 4);
        }
        ClientSoftware clientSoftware = clientSoftwareService.getLatestVersion(queryMo);
        if (Objects.isNull(clientSoftware)) {
            ro.setMessage("未查询到数据");
        } else {
            ro.setContent(clientSoftware);
        }
        return ro;
    }

    @GetMapping(value = "/isLatestVersion")
    @Permission(value = 0, tags = Constants.API_COMMON)
    public Ro<Boolean> isLatestVersion(@RequestBody QueryMo queryMo) {
        return new Ro<Boolean>().setContent(clientSoftwareService.isLatestVersion(queryMo));
    }

    @PostMapping(value = "/admin/insert")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<ClientSoftware> insert(@RequestBody OperateMo operateMo) {
        Ro<ClientSoftware> ro = new Ro<>();
        operateMo.createBuild(ClientSoftware.class, null);
        if (operateMo.getEntity().size() == 0) {
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        String versionNumber = (String) operateMo.get("versionNumber");
        String type = String.valueOf(operateMo.get("type"));
        String url = (String) operateMo.get("url");
        if (Objects.nonNull(type)) {
            if (!("1".equals(type) || "2".equals(type) || "3".equals(type) || "4".equals(type))) {
                return ro.setResultEnum(RoState.FAIL).setMessage("类型错误");
            }
        } else {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[type]字段,请传参后重试");
        }
        if (Objects.nonNull(versionNumber)) {
            QueryMo queryMo = new QueryMo();
            queryMo.put("versionNumber", versionNumber);
            queryMo.put("type", type);
            queryMo.build(ClientSoftware.class, null);
            if (Objects.nonNull(clientSoftwareService.selectOne(queryMo))) {
                return ro.setResultEnum(RoState.FAIL).setMessage("版本号已存在");
            }
        } else {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[versionNumber]字段,请传参后重试");
        }
        if (Objects.nonNull(url)) {
            if (validateModel.uploadClientValidate(url)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[url],文件不存在或文件上传类别错误");
            }
        }
        System.out.println(444);
        if (!clientSoftwareService.insert(operateMo)) {
            ro.setResultEnum(RoState.FAIL);
        }
        return ro;
    }

    @PatchMapping(value = "/admin/modify")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<ClientSoftware> modify(@RequestBody OperateMo operateMo) {
        Ro<ClientSoftware> ro = new Ro<>();
        final ServiceRo serviceRo = operateMo.updateBuild(ClientSoftware.class, null);
        if (!serviceRo.getResult()) {
            return ro.setResultEnum(RoState.FAIL).setMessage(serviceRo.getMessage());
        }
        if (operateMo.getEntity().size() == 0) {
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (Objects.isNull(operateMo.get("id"))) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[id]字段,请传参后重试");
        }
        String versionNumber = (String) operateMo.get("versionNumber");
        String url = (String) operateMo.get("url");
        String type = String.valueOf(operateMo.get("type"));
        Integer id = (Integer) operateMo.get("id");
        if (Objects.nonNull(type)) {
            if (!("1".equals(type) || "2".equals(type) || "3".equals(type) || "4".equals(type))) {
                return ro.setResultEnum(RoState.FAIL).setMessage("类型错误");
            }
        } else {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[type]字段,请传参后重试");
        }
        if (Objects.nonNull(versionNumber)) {
            QueryMo queryMo = new QueryMo();
            queryMo.put("versionNumber", versionNumber);
            queryMo.put("type", type);
            queryMo.build(ClientSoftware.class, null);
            final ClientSoftware clientSoftware = clientSoftwareService.selectOne(queryMo);
            if (Objects.nonNull(clientSoftware) && !clientSoftware.getId().equals(id)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("版本号已存在");
            }
        }
        if (Objects.nonNull(url)) {
            if (validateModel.uploadClientValidate(url)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[url],文件不存在或文件上传类别错误");
            }
        }
        if (!clientSoftwareService.update(operateMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("参数无匹配,修改失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<ClientSoftware> delete(@PathVariable Integer id) {
        Ro<ClientSoftware> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(ClientSoftware.class, null);
        if (Objects.nonNull(queryMo.get("state"))) {
            queryMo.getEqMap().put("state", queryMo.get("state"));
        } else {
            queryMo.getEqMap().remove("state");
        }
        if (!clientSoftwareService.delete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/logical/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<ClientSoftware> logicallyDelete(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        Ro<ClientSoftware> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(ClientSoftware.class, null);
        if (!clientSoftwareService.logicallyDelete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }
}